/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package features;

/**
 *
 * @author Tuxá
 */
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.JToolBar;

import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;

/**
 * Prompts the user for a shapefile and displays the contents on the screen in a map frame.
 * <p>
 * This is the GeoTools Quickstart application used in documentationa and tutorials. *
 */
public class ImprimirPDF {
    private JMapFrame mapFrame;

    public ImprimirPDF() {
    }

    public static void main(String[] args) throws Exception {
        // Create a map context and add our shapefile to it
        final ImprimirPDF quick = new ImprimirPDF();

       

        quick.setMapFrame(new JMapFrame());
        quick.getMapFrame().setTitle("Visualizador SIG");
        quick.getMapFrame().enableToolBar(true);
        quick.getMapFrame().enableStatusBar(true);

        JToolBar toolbar =  quick.getMapFrame().getToolBar();
        toolbar.addSeparator();
        JButton botao = new JButton();
        toolbar.add(botao);

        botao.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                try {
                   quick.abrirMapa();
                }catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });

        quick.getMapFrame().setSize(800, 600);
        quick.getMapFrame().setVisible(true);

    }

    public void abrirMapa() throws IOException{
         // display a data store file chooser dialog for shapefiles
        final MapContext map = new DefaultMapContext();
        File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }


        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        SimpleFeatureSource featureSource = store.getFeatureSource();

        map.addLayer(featureSource, null);
        // Now display the map
        mapFrame = new JMapFrame(map);
        mapFrame.setMapContext(map);
        mapFrame.setRenderer(new StreamingRenderer());

        return;
    }

    public JMapFrame getMapFrame() {
        return mapFrame;
    }

    public void setMapFrame(JMapFrame mapFrame) {
        this.mapFrame = mapFrame;
    }

}
